﻿using Engine.FileFormats3D.WR2TerrainLoader.Misc;
using Engine.Graphics;
using System.Collections.Generic;
using System.IO;
using WorldRacing2.Material;

namespace WorldRacing2.Exporter
{
    public class ExportMaterialListSTKit2
    {
        #region Public Methods

        public static void Export(string filename, List<TexturePropertie> textureProperties, MaterialManager materialManager)
        {
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            char[] stkitHeader = new char[] { 'S', 'T', 'K', 'i', 't', '2' };
            short version = 256;

            bw.Write(stkitHeader);
            bw.Write(version);
            bw.Write(materialManager.GetNumberOfMaterials());

            byte[] materialMode = new byte[110];
            for (int i = 0; i < materialManager.GetNumberOfMaterials(); i++)
            {
                char[] textureName = new char[32];
                ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName1.ToCharArray().CopyTo(textureName, 0);
                bw.Write(textureName);
                textureName = new char[32];
                ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName1.ToCharArray().CopyTo(textureName, 0);
                bw.Write(textureName);
                textureName = new char[32];
                ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName2.ToCharArray().CopyTo(textureName, 0);
                bw.Write(textureName);
                textureName = new char[32];
                ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName3.ToCharArray().CopyTo(textureName, 0);
                bw.Write(textureName);

                bw.Write(materialMode);
            }

            // Write the grass value.
            bw.Write("GROWGRAS".ToCharArray());
            for (int i = 0; i < materialManager.GetNumberOfMaterials(); i++)
            {
                bool hasGrasTex1 = false;
                bool hasGrasTex2 = false;
                bool hasGrasTex3 = false;

                for (int j = 0; j < textureProperties.Count; j++)
                {
                    if (textureProperties[j].TextureName.ToUpper() == ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName1.ToUpper())
                    {
                        if (!hasGrasTex1)
                        {
                            hasGrasTex1 = textureProperties[j].HasGras;
                            if (hasGrasTex1)
                                break;
                        }
                    }
                    if (textureProperties[j].TextureName.ToUpper() == ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName2.ToUpper())
                    {
                        if (!hasGrasTex2)
                        {
                            hasGrasTex2 = textureProperties[j].HasGras;
                            if (hasGrasTex2)
                                break;
                        }
                    }
                    if (textureProperties[j].TextureName.ToUpper() == ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName3.ToUpper())
                    {
                        if (!hasGrasTex3)
                        {
                            hasGrasTex3 = textureProperties[j].HasGras;
                            if (hasGrasTex3)
                                break;
                        }
                    }
                }

                if (hasGrasTex1 || hasGrasTex2 || hasGrasTex3)
                    bw.Write(true);
                else
                    bw.Write(false);
            }

            // Write the grass value.
            bw.Write("ENLITE00".ToCharArray());
            for (int i = 0; i < materialManager.GetNumberOfMaterials(); i++)
            {
                bool hasGrasTex1 = false;
                bool hasGrasTex2 = false;
                bool hasGrasTex3 = false;

                for (int j = 0; j < textureProperties.Count; j++)
                {
                    if (textureProperties[j].TextureName.ToUpper() == ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName1.ToUpper())
                    {
                        if (!hasGrasTex1)
                            hasGrasTex1 = textureProperties[j].Enlite;
                    }
                    if (textureProperties[j].TextureName.ToUpper() == ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName2.ToUpper())
                    {
                        if (!hasGrasTex2)
                            hasGrasTex2 = textureProperties[j].Enlite;
                    }
                    if (textureProperties[j].TextureName.ToUpper() == ((Wr2Material)materialManager.GetMaterial(i).GameMaterialProperties).TextureName3.ToUpper())
                    {
                        if (!hasGrasTex3)
                            hasGrasTex3 = textureProperties[j].Enlite;
                    }
                }

                if (hasGrasTex1 || hasGrasTex2 || hasGrasTex3)
                    bw.Write(true);
                else
                    bw.Write(false);
            }

            bw.Close();
            fs.Close();
        }

        #endregion Public Methods
    }
}